const { Md5Crypto } = require('../util/crypto')
const { signToken } = require('../util/token')
const { getUser, addUser, verifyUser,changePassword,changeStatus,editUser } = require('../dao/user')

// 登录
function UserLogin(params) {
    return new Promise(async (resolve, reject) => {
        const { account, password} = params
        // 账号登录 
            if (!account || !password) {
                resolve(fail('未传递账号或密码'))
                return
            }
        verifyUser({ account, password: Md5Crypto(password, account) }).then(user => {
                if (!user) {
                    resolve(fail('账号或密码错误'))
                } else {
                    const { user_id } = user
                    signToken({ user_id }).then(token => {
                        delete user['password']
                        resolve(ok({
                            data: { token, info: user }
                        }))
                    }).catch(err => {
                        resolve(error(`服务异常:${err.message}`))
                    })
                }
            }).catch(err => {
                resolve(error(err))
            })
    })
}

//注册新用户
function UserRegister(params) {
    return new Promise((resolve, reject) => {
        const { account, password,stu_num,name } = params
        //console.log(account,password)
        if (!account || !password) {
            resolve(fail('未传递账号或密码'))
            return
        }
        if (!stu_num) {
            resolve(fail('未传递学号'))
            return
        }
        if (!name) {
            resolve(fail('未传递姓名'))
            return
        }
            addUser({ account, password: Md5Crypto(password, account) ,stu_num,name}).then(() => {
                resolve(ok())
            }).catch(err => {
                resolve(error(err))
            })
        })
}

//用户修改密码
function UserChangePassword(params){
    return new Promise((resolve, reject) => {
        const {user_id,password,new_password,account}=params
        if(!user_id){
            resolve(fail('未传递用户id,有可能需要重新登陆'))
            return
        }
        if(!password){
            resolve(fail('未传递旧密码'))
            return
        }
        if(!new_password){
            resolve(fail('未传递新密码'))
            return
        }
        verifyUser({account,password: Md5Crypto(password,account)}).then(user => {
            if (!user) {
                resolve(fail('账号或密码错误'))
            }else{
                 console.log(user_id)
                changePassword({user_id,password: Md5Crypto(new_password,account)}).then(()=>{
                    resolve(ok({msg:"密码修改成功"}))
                }).catch(err => {
                    resolve(error(err))
                })
            }
        })

    })
}

//退出登录
function UserLogout(params) {
    return new Promise((resolve, reject) => {
        const {user_id} = params
        if(!user_id){
            resolve(fail('未传递用户id,有可能需要重新登陆'))
            return
        }
        changeStatus({user_id}).then(()=>{
          resolve(ok({msg:"退出登录成功"}))
      }).catch(err => {
          resolve(error(err))
     })

    })
}

// 获取用户信息
function UserGetInfo(params) {
    return new Promise((resolve, reject) => {
      getUser(params)
        .then(data => {
          resolve(ok({ data: { status: data.status, name: data.name, avatar: data.avatar, sex: data.sex, faculty: data.faculty, introduction: data.introduction } }));
        })
        .catch(err => {
          resolve(error(err))
        });
    })
  }
// 修改用户信息
function UserEditMyInfo(params) {
    return new Promise((resolve, reject) => {
      // 调用 updateUser 函数
      editUser(params)
        .then(data => {
          resolve(ok({ data: { status: data.status, name: data.name, avatar: data.avatar, sex: data.sex, faculty: data.faculty, introduction: data.introduction } }));
        })
        .catch(err => {
          resolve(error(err))
        });
  
    });
  }
module.exports = {
    UserLogin,UserRegister,UserChangePassword,UserLogout,UserEditMyInfo,UserGetInfo
}